var imgdir = imgdir || '';

var pc2009 = pc2009 || {};
pc2009.newBlock = function(x, y, color) {
	// var
	var m_x = x;
	var m_y = y;
	var m_color = color;

	var m_DOM = jQuery('<img border="0" style="position: absolute; width: 32px; height: 32px; z-index: 1;" />');
	m_DOM.css('left', m_x*32).css('top', m_y*32).attr('src', imgdir+'/'+color+'.png');
	// method
	var m_move = function(x, y) {
		m_x = x;
		m_y = y;
		m_DOM.animate({left:x*32, top:y*32}, 500, 'linear');
	};
	// init
	m_DOM.move = m_move;
	return m_DOM;
};

var pc2009 = pc2009 || {};
pc2009.newMouseSensor = function(x, y, callback, over_callback) {
	// var
	var m_DOM = jQuery('<img src="'+imgdir+'/s.png" style="position: absolute; width: 32px; height: 32px; z-index: 100;" />');
	m_DOM.css('left', x*32 - 16).css('top', y*32 - 16);
	m_DOM.click(function(){
		if(callback){
			callback(x, y);
		}
	});
	m_DOM.mouseover(function(){
		if(over_callback){
			over_callback(x, y);
		}
	});
	return m_DOM;
};


var pc2009 = pc2009 || {};
pc2009.newBoard = function(map, callback) {
	// var
	var m_callback = callback;
	var m_DOM = jQuery('<div style="position: relative;"></div>');
	var m_border = jQuery('<img src="'+imgdir+'/s.png" style="position: absolute; z-index: 50; border: 3px solid black;" />');
	var m_border_move = function(x, y, size){
		m_border.hide();
		m_border.css('width', 32*size);
		m_border.css('height', 32*size);
		m_border.css('left', x*32-size*16-3);
		m_border.css('top', y*32-size*16-3);
		m_border.show();
	};
	var m_border_hide = function(){
		m_border.hide();
	};
	m_border_hide();
	m_DOM.append(m_border);

	var m_size = map.length;
	var m_r = 1;
	var m_map = [];
	var m_init_map = [];
	// method
	var m_setR = function(r){
		if(r<2){
			r = 2;
		}else if(18<r){
			r = 18;
		}else{
			r &= ~1;
		}
		m_r = r / 2;
		m_border_hide();
	};
	var m_map_copy = function(){
		m_map = [];
		for(var y=0; y<m_size; ++y){
			m_map.push(m_init_map[y].slice(0));
		}
	}
	var m_clear_map = function(){
		m_map_copy();
		for(var y=0; y<m_size; ++y){
			for(var x=0; x<m_size; ++x){
				m_map[y][x].move(x, y);
			}
		}
	}
	var m_rotate = function(x, y, r){
		if(r<=x && r<=y && x<=m_size-r && y<=m_size-r){
			var xx = x * 2 - 1;
			var yy = y * 2 - 1;
			for(var dy=1; dy<r*2; dy+=2){
				for(var dx=1; dx<r*2; dx+=2){
					var dmy = m_map[(yy+dy)/2][(xx+dx)/2];
					m_map[(yy+dy)/2][(xx+dx)/2] = m_map[(yy-dx)/2][(xx+dy)/2];
					m_map[(yy-dx)/2][(xx+dy)/2] = m_map[(yy-dy)/2][(xx-dx)/2];
					m_map[(yy-dy)/2][(xx-dx)/2] = m_map[(yy+dx)/2][(xx-dy)/2]
					m_map[(yy+dx)/2][(xx-dy)/2] = dmy;
					m_map[(yy+dy)/2][(xx+dx)/2].move((xx+dx)/2, (yy+dy)/2);
					m_map[(yy-dx)/2][(xx+dy)/2].move((xx+dy)/2, (yy-dx)/2);
					m_map[(yy-dy)/2][(xx-dx)/2].move((xx-dx)/2, (yy-dy)/2);
					m_map[(yy+dx)/2][(xx-dy)/2].move((xx-dy)/2, (yy+dx)/2);
				}
			}
			return true;
		}
		return false;
	};
	var m_undo = function(line){
		if(line!=null){
			var x = line[0];
			var y = line[1];
			var r = line[2] / 2;
			var d = line[3];
			for(var i=d; i<4; ++i){
				m_rotate(x, y, r);
			}
		}
	};
	var m_click_event = function(x, y){
		if(m_rotate(x, y, m_r) && m_callback){
			m_callback(x, y, m_r*2);
		}
	};
	var m_over_event = function(x, y){
		if(m_r<=x && m_r<=y && x<=m_size-m_r && y<=m_size-m_r){
			m_border_move(x, y, m_r * 2);
		} else {
			m_border_hide();
		}
	};
	//init
	(function() {
		m_DOM.css('width', m_size*32).css('height', m_size*32);
		for(var y=0; y<m_size; ++y) {
			var m = [];
			for(var x=0; x<m_size; ++x) {
				var b = pc2009.newBlock(x, y, map[y][x]);
				m[x] = b;
				m_DOM.append(b);
			}
			m_init_map[y] = m;
		}
		for(var y=1; y<m_size; ++y) {
			for(var x=1; x<m_size; ++x) {
				var b = pc2009.newMouseSensor(x, y, m_click_event, m_over_event);
				m_DOM.append(b);
			}
		}
		m_map_copy();
	}());
	m_DOM.setR = m_setR;
	m_DOM.clear = m_clear_map;
	m_DOM.undo = m_undo;
	return m_DOM;
};

var pc2009 = pc2009 || {};
pc2009.newMemo = function() {
	// var
	var m_DOM = jQuery('<textarea cols="15" rows="10" readonly></textarea>');
	var m_list = [];
	// method
	var m_clear = function(){
		m_list = [];
		m_DOM.val('');
	}
	var m_adjust_dom = function(){
		var csv = "";
		for(var i=0; i<m_list.length; ++i){
			var v = m_list[i];
			csv += v[0]+","+v[1]+","+v[2]+","+v[3]+"\n";
		}
		m_DOM.val(csv);
	}
	var m_reg = function(x, y, r){
		var len = m_list.length;
		if(len!=0 && m_list[len-1][0]==x && m_list[len-1][1]==y && m_list[len-1][2]==r){
			if(m_list[len-1][3]!=3){
				++m_list[len-1][3];
			}else{
				m_list.pop();
			}
		}else{
			m_list.push([x, y, r, 1]);
		}
		m_adjust_dom();
	};
	var m_undo = function(){
		if(m_list.length==0){
			return null;
		}
		result = m_list.pop();
		m_adjust_dom();
		return result;
	};
	// init
	m_DOM.regist = m_reg;
	m_DOM.clear = m_clear;
	m_DOM.undo = m_undo;
	return m_DOM;
};

var pc2009 = pc2009 || {};
pc2009.newSizeSetting = function(callback) {
	// var
	var m_r = 2;
	var m_DOM = jQuery('<table></table>');
	var tr = jQuery('<tr></tr>');
	var td1 = jQuery('<td></td>');
	var td2 = jQuery('<td>2</td>');
	var td3 = jQuery('<td></td>');
	var m_l_img = jQuery('<img src="'+imgdir+'/l.png" />');
	var m_r_img = jQuery('<img src="'+imgdir+'/r.png" />');
	td1.append(m_l_img);
	td3.append(m_r_img);
	tr.append(td1);
	tr.append(td2);
	tr.append(td3);
	m_DOM.append(tr);
	m_l_img.click(function(){
		m_r -= 2;
		if(m_r<2){
			m_r = 2;
		}
		td2.html(m_r);
		callback(m_r);
	});
	m_r_img.click(function(){
		m_r += 2;
		if(18<m_r){
			m_r = 18;
		}
		td2.html(m_r);
		callback(m_r);
	});
	// method
	// init
	return m_DOM;
};

var pc2009 = pc2009 || {};
pc2009.newButtonBar = function(list) {
	// var
	var m_DOM = jQuery('<div></div>');
	(function(){
		for(var i=0; i<list.length; ++i){
			if(i!=0){
				m_DOM.append(' ');
			}
			var m_DOM_img = jQuery('<img src="'+list[i][0]+'" />');
			m_DOM_img.click(list[i][1]);
			m_DOM.append(m_DOM_img);
		}
	}());
	return m_DOM;
};

